<?php

/**
 * @property PDO $conn
 */
abstract class ADatabase
{
	private $_conn;
	
	public static function create(ConfigServer $configServer)
	{
		switch($configServer->driver)
		{
			case ConfigServer::DRIVER_MYSQL:
			default:
				$db = new MysqlDatabase();
				$db->_conn = new \PDO("mysql:host=".$configServer->hostname.";dbname=".$configServer->schema, $configServer->username, $configServer->password);
		}
		return $db;
	}
	
	public function __get($var)
	{
		switch($var)
		{
			case "conn": return $this->_conn;
		}
	}
	
	abstract public function getDatabases();
	abstract public function getTables($database);
	
	abstract public function useDatabase($database);
	abstract public function mainPanelServer(ConfigServer $server);
	abstract public function mainPanelDatabase($database);
	/** @return DatabaseResult */
	abstract public function tableColumns($database, $table);
	/** @return DatabaseResult */
	abstract public function tableIndexes($database, $table);
	/** @return DatabaseResult */
	abstract public function browseResult($database, $table, $perPage, $page);
	/**
	 * @return DatabaseResult
	 */
	public function getQueryResult($query)
	{
		$result = new DatabaseResult();
		$result->query = $query;
		$start = \microtime(true);
		$result->statement= $this->conn->prepare($query);
		$result->success = $result->statement->execute();
		$end = \microtime(true);
		$result->time = $end - $start;
		$result->lastInsertId = $this->conn->lastInsertId();
		$result->database = $this;
		$result->resultRows = (int)$result->statement->rowCount();
		return $result;
	}
}